import java.util.Scanner;

/**
 * This program allows a user to input two fractions and computes their sum 
 * and appropriately handles errors.
 * (Lab #3)
 * 
 * @author Tara Gallagher
 *
 */
public class Lab03 {

	public static void main(String[] args) {

		// Read and interpret the input.
		Scanner input = new Scanner(System.in);
		System.out.print("Enter first fraction: ");
		String fraction1 = input.nextLine();
		if (fraction1.indexOf('/') == -1) { // Check input format.
			System.out.print("A '/' must separate the fraction.");
			System.exit(1);
			} else {
				// Use '/' to separate numerator and denominator.
				int separator1 = fraction1.indexOf('/');
				String num1s = fraction1.substring(0, separator1);
				num1s = num1s.trim();
				String den1s = fraction1.substring(separator1 + 1,
					fraction1.length());
				den1s = den1s.trim();
				int num1 = Integer.parseInt(num1s);
				int den1 = Integer.parseInt(den1s);
				if (den1 == 0) { // Make sure the fraction is defined.
					System.out.print("The denominator cannot equal 0.");
					System.exit(1);
			} else {

		System.out.print("Enter second fraction: ");
		String fraction2 = input.nextLine();
		if (fraction2.indexOf('/') == -1) { // Check input format.
		System.out.print("A '/' must separate the fraction.");
		System.exit(1);
			} else { 
				// Use '/' to determine numerator and denominator.
				int separator2 = fraction2.indexOf('/');
				String num2s = fraction2.substring(0, separator2);
				num2s = num2s.trim();
				int num2 = Integer.parseInt(num2s);
				String den2s = fraction2.substring(separator2 + 1,
							fraction2.length());
				den2s = den2s.trim();
				int den2 = Integer.parseInt(den2s);
				if (den2 == 0) { // Make sure the fraction is defined.
					System.out.printf("The denominator cannot equal 0.");
					System.exit(1);
					input.close();
				} 
				else {
						// Compute the sums.
						int numSum;
						int denSumModified;
						int numSumModified;
						numSum = num1 + num2;

						// Adding with uncommon denominators.
						if (den1 != den2) { 
							numSumModified = (num1 * den2) + (num2 * den1); 
							denSumModified = den1 * den2; 
							if (numSumModified == denSumModified) {
								System.out.printf("The sum is 1.");
							// Make sure the denominator is positive.
							} else if (denSumModified < 0) { 
								denSumModified *= -1;
								numSumModified *= -1;
								System.out.printf("The sum is: %d / %d",
										numSumModified, denSumModified);
							} else
								System.out.printf("The sum is: %d / %d",
										numSumModified, denSumModified);
						}
						// Adding with common denominators.
						else if (den1 == den2 && numSum != den1) 
							// Make sure the denominator is positive.
							if (den1 < 0) {
								den1 *= -1;
								numSum *= -1;
								System.out.printf("The sum is %d / %d", numSum,den1);
							} else
								System.out.printf("The sum is %d / %d", numSum,den1);
						else
							System.out.printf("The sum is 1.");
					}
				}
			}
		}
	}
}
